package cn.edu.swu.service;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Classname GrabLessons
 * @Description TODO
 * @Date 2021/9/5 23:44
 * @Author Lenovo
 * @Modified by
 */
@Service
public class GrabLessonsService {

    public void grab(String account,String pwd) throws Exception{
        System.setProperty("webdriver.chrome.driver","src/main/resources/chromedriver.exe");
        ChromeDriver chromeDriver = new ChromeDriver();
        chromeDriver.get("http://www.swu.edu.cn/");
        WebElement index4_r1_c2 = chromeDriver.findElement(By.id("index4_r1_c2"));
        index4_r1_c2.click();

        String windowHandle1 = chromeDriver.getWindowHandle();
        Set<String> windowHandles1 = chromeDriver.getWindowHandles();
        for (String s:windowHandles1) {
            if (s.equals(windowHandle1)) continue;
            try {
                chromeDriver.switchTo().window(s);
                chromeDriver.findElement(By.id("username"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        WebElement username = chromeDriver.findElement(By.id("username"));
        username.sendKeys(account);
        WebElement password = chromeDriver.findElement(By.id("password"));
        password.sendKeys(pwd);
        chromeDriver.findElement(By.linkText("登录")).click();

        try {
            chromeDriver.findElement(By.xpath("//a[3]/img")).click();
        } catch (Exception e) {
            throw new Exception("error");
        }

        String windowHandle2 = chromeDriver.getWindowHandle();
        Set<String> windowHandles2 = chromeDriver.getWindowHandles();
        for (String s : windowHandles2) {
            if (s.equals(windowHandle1)) continue;
            if (s.equals(windowHandle2)) continue;
            chromeDriver.switchTo().window(s);
        }

        chromeDriver.findElement(By.linkText("选课")).click();
        chromeDriver.findElement(By.linkText("自主选课")).click();

        String windowHandle3 = chromeDriver.getWindowHandle();
        Set<String> windowHandles3 = chromeDriver.getWindowHandles();
        for (String s : windowHandles3) {
            if (s.equals(windowHandle1)) continue;
            if (s.equals(windowHandle2)) continue;
            if (s.equals(windowHandle3)) continue;
            chromeDriver.switchTo().window(s);
        }

        chromeDriver.findElement(By.xpath("//button[@name='query']")).click();

        List<WebElement> elements = chromeDriver.findElements(By.cssSelector(".panel-info"));
        elements.remove(0);
        int i = 0;

        for (WebElement e : elements) {
            i++;
            String kchId = e.findElement(By.name("kch_id")).getAttribute("value");
            String text = e.findElement(By.id("zt_txt_" + kchId)).getText();
            text=text.substring(text.length()-2);
            if ("已选".equals(text)) {
                continue;
            }
            if (i!=1) {
                WebElement element = e.findElement(By.cssSelector(".expand_close"));
                element.click();
            }
            try{
                TimeUnit.SECONDS.sleep(2);
            }catch (InterruptedException exception){
                exception.printStackTrace();
            }
            WebElement button = e.findElement(By.tagName("button"));

            try{
                button.click();
            }catch (Exception exception){
                exception.printStackTrace();
            }

            WebElement alert = null;
            try {
                alert = chromeDriver.findElement(By.cssSelector(".modal-content"));
            } catch (Exception ex) {
                continue;
            }
            List<WebElement> buttons = alert.findElements(By.cssSelector(".btn-primary"));
            WebElement buttonSuccess = null;
            WebElement buttonChoose = null;

            for (WebElement but : buttons) {
                String buttonId = but.getAttribute("id");
                if ("btn_success".equals(buttonId)){
                    buttonSuccess=but;
                }else{
                    buttonChoose=but;
                }
            }
            if (buttonChoose!=null)
                buttonChoose.click();
            if (buttonSuccess!=null)
                buttonSuccess.click();
        }

        for (String we : windowHandles3) {
            if (!windowHandle1.equals(we)){
                chromeDriver.switchTo().window(we).close();
            }
        }

    }
}
